package edu.xhu.adapters;

import java.util.ArrayList;

import com.bean.JobBean;

import edu.xhu.R;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ChuanDaJobAdapter extends BaseAdapter{

	private ArrayList<JobBean> jobBeans;
	private LayoutInflater inflater;
	private Context context;
	
	public ChuanDaJobAdapter(Context context, ArrayList<JobBean> jobBeans){
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.jobBeans = jobBeans;
	}
	
	public void addItems(ArrayList<JobBean> jobBeans){
		this.jobBeans.addAll(jobBeans);
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		if (null == jobBeans)
			return 0;
		return jobBeans.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (null == convertView){
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.chuanda_job_item, null);
			viewHolder.contextTextView = (TextView) convertView.findViewById(R.id.textview_info);
			viewHolder.share = (ImageView) convertView.findViewById(R.id.imageview_share);
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		JobBean bean = jobBeans.get(position);
		viewHolder.contextTextView.setText(bean.title);
		viewHolder.share.setOnClickListener(new ShareListener(position));
		
		return convertView;
	}

	class ViewHolder{
		public TextView contextTextView;
		public ImageView share;
	}
	
	class ShareListener implements OnClickListener{
		private int pos;
		public ShareListener(int pos){
			this.pos = pos;
		}
		@Override
		public void onClick(View v) {
			JobBean tmpBean = jobBeans.get(pos);
			String content = "四川大学招聘信息：" + tmpBean.url + "\n"
					+" \"" + tmpBean.title + "\""
					+ " ||来自掌中西华:http://www.esjiaoyi.com/zzxh/";
			
			Intent intent=new Intent(Intent.ACTION_SEND);
	        intent.setType("text/plain");
	        intent.putExtra(Intent.EXTRA_SUBJECT, "数据的标题");
	        intent.putExtra(Intent.EXTRA_TEXT, content);
	        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        context.startActivity(Intent.createChooser(intent, "分享"));
		}
	}
}
